package cn.bocweb.spinning.view.fragment;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Spinner;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.activity.SelectActivity;

/**
 * Created by chenchangxing on 16/3/9.
 */
public class ProductCompanyFragment extends Fragment implements View.OnClickListener {

    ProductListFragment productListFragment;
    FragmentManager fragmentManager;
    CompanyListFragment companyListFragment;
    int type = Constant.PRODUCT;

    @Bind(R.id.product_company_search)
    LinearLayout productCompanySearch;
    @Bind(R.id.product_main_spinner)
    Spinner productMainSpinner;
    @Bind(R.id.product_company_layout)
    FrameLayout productCompanyLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product_company, container, false);
        ButterKnife.bind(this, view);
        productListFragment = new ProductListFragment();
        companyListFragment = new CompanyListFragment();
        fragmentManager = getChildFragmentManager();

        initToolbar();
        initClickBtn();

        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.product_company_search: {
                Intent intent = new Intent(getActivity(), SelectActivity.class);
                intent.putExtra(Constant.TO_SEARCH, type);
                startActivity(intent);
                break;
            }
        }
    }

    private void initClickBtn() {
        productCompanySearch.setOnClickListener(this);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void initToolbar() {
        productMainSpinner.setDropDownVerticalOffset(DeviceUtil.dip2px(getActivity(), 50));
        productMainSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0: {
                        type = Constant.PRODUCT;
                        if (!productListFragment.isAdded()) {
                            fragmentManager.beginTransaction().hide(productListFragment).hide(companyListFragment).add(R.id.product_company_layout, productListFragment).show(productListFragment).commit();
                        } else {
                            fragmentManager.beginTransaction().hide(productListFragment).hide(companyListFragment).show(productListFragment).commit();
                        }
                        break;
                    }
                    case 1: {
                        type = Constant.COMPANY;
                        if (!companyListFragment.isAdded()) {
                            fragmentManager.beginTransaction().hide(productListFragment).hide(companyListFragment).add(R.id.product_company_layout, companyListFragment).show(companyListFragment).commit();
                        } else {
                            fragmentManager.beginTransaction().hide(productListFragment).hide(companyListFragment).show(companyListFragment).commit();
                        }
                        break;
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
